package Servlet;

import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.*;
import Entity.*;

public class AddPayProveServlet extends HttpServlet {

    private UserListDao userdao = new UserListDao();
    private AddPayProveDao addpayprovedao = new AddPayProveDao();
    private ProveNumDao numdao = new ProveNumDao();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        String path = "pages/Pay_Prove.jsp";
        boolean ac = false;

        if (method.equals("list")) {
            //获得所有未冻结的账户
            List<UserListEntity> list_user = userdao.select_showd();
            request.setAttribute("list_user", list_user);
            request.setAttribute("pay_num", numdao.select().get(0).getPay_num());
        } else if (method.equals("add")) {
            String jsp_Time = request.getParameter("pay_form_time");

            //获得上一个凭证的编号
            int num = numdao.select().get(0).getPay_num();
            numdao.update_pay(num + 1);
            String operator = request.getParameter("form_operator");
            //获得 当前 系统 时间
            java.util.Date date = new java.util.Date();
            Timestamp time = new Timestamp(date.getTime());
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timer = df.format(time);

            PayProveEntity payentity = new PayProveEntity();
            payentity.setNum(num);
            payentity.setName(request.getParameter("pay_form_name"));
            payentity.setCode(Integer.parseInt(request.getParameter("pay_form_code")));
            //系统时间--当前时间
            //payentity.setTime(timer);
            //前段时间--当前 或 自定义时间
            payentity.setTime(jsp_Time);
            payentity.setPay_total(Double.parseDouble(request.getParameter("pay_form_pay_total")));
            payentity.setSurplus(Double.parseDouble(request.getParameter("pay_form_surplus")));
            payentity.setOperator(operator);
            payentity.setProject(1);
            addpayprovedao.add(payentity);
            //更改用户的账户余额
            userdao.add_user_surplus(Double.parseDouble(request.getParameter("pay_form_surplus")), Integer.parseInt(request.getParameter("pay_form_code")));
            path = "Add_Pay_Prove_Servlet?method=list";
            ac = true;
        }
        if (ac) {
            response.sendRedirect(path);
        } else {
            request.getRequestDispatcher(path).forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
